express = require 'express'
router = express.Router()

router.get '/', (req, res, next) ->
  Promise.all([
    req.api.v4.get
      'action': 'corpcredit.course.progress.m.list'
      'filterUserName': req.query.filterUserName ? ''
      'filterStatus': req.query.filterStatus ? 0
      'filterCheckPass': req.query.filterJob ? 1
      'companyID': req.companyId
      'pageIndex': req.query.pageIndex ? 1
      'pageSize': req.query.pageSize ? 10
      'sessionkey': req.cookies['__t'] ? 'sessionKey'
    req.api.v4.get
      'action': 'corpcourse.CompanyCourseStats.list'
      'companyId':req.companyId
      'sessionkey': req.cookies['__t'] ? 'sessionKey'
      'pageSize': req.query.hotPageSize ? 12
      'pageNo': req.query.hotPageIndex ? 1
  ])
  .then (results) ->
    if results[0].status && results[1].status
      res.render 'index' ,{datas:results[0].data.list,query:req.query,hotData:results[1].data.rps}
    else
      console.log '未登录'
      return res.render '404'
  .catch next

router.get '/learningRecord-:userId', (req, res, next) ->
  Promise.all([
    req.api.v4.get
      'action': 'corpcredit.course.progress.m.list'
      'filterUserId': req.params.userId
      'pageIndex': req.query.pageIndex ? 1
      'pageSize': req.query.pageSize ? 10
      'companyId': req.companyId
      'sessionkey': req.cookies['__t'] ? 'sessionKey'
    req.api.v4.get
      'action': 'corpcredit.creditanalyze.gettotal'
      'userId': req.params.userId
      'companyId': req.companyId
      'sessionkey': req.cookies['__t'] ? 'sessionKey'
    req.api.v4.get
      'action': 'corpcourse.comment.ListByUser'
      'userId': req.params.userId
      'companyId': req.companyId
      'sessionkey': req.cookies['__t'] ? 'sessionKey'
      'pageIndex': req.query.commentPageIndex ? 1
      'pageSize': req.query.commentPageSize ? 12
  ])
  .then (results) ->
    if results[0].status && results[1].status && results[2].status
      res.render 'learningRecord' ,{datas:results[0].data.list,query:req.query,addData:results[1].data.totalCredit,commentData:results[2].data.comments,params:req.params}
    else
      console.log '未登录'
      return res.render '404'
  .catch (err) ->
    console.log err
    next err

router.get '/playRecord-:userId-:courseId-:isYS', (req, res, next) ->
  req.api.v4.get {
    action: 'corpcredit.course.video.progress.m.list'
    isYS: req.params.isYS
    userId: req.params.userId
    courseId: req.params.courseId
    companyId: req.companyId
    sessionkey: req.cookies['__t'] ? 'sessionKey'
  }
  .then (data) ->
    if not data.status
      return res.render '404'
    res.render 'playRecord',{datas:data.data.list,companyId:req.companyId,query:req.query,params:req.params}
  .catch next
router.get '/playDetails-:userId-:courseId-:videoId-:isYS', (req, res, next) ->
  req.api.v4.get {
    action: 'corpcredit.watch.detail.m.list'
    userId: req.params.userId
    courseId: req.params.courseId
    companyId: req.companyId
    sessionkey: req.cookies['__t'] ? 'sessionKey'
    videoId: req.params.videoId
    pageIndex: req.query.pageIndex ? 1
    pageSize: req.query.pageSize ? 10
  }
  .then (data) ->
    if not data.status
      return res.render '404'
    res.render 'playDetails',{datas:data.data.list,companyId:req.companyId,userId:req.params.userId,query:req.query,params:req.params}
  .catch next

router.post '/corpcourseCommentList', (req, res, next) ->
  params = req.body
  req.api.v4.get {
    action: 'corpcourse.comment.list'
    companyId: req.companyId
    userId: params.userId ? ''
    courseId: params.courseId ? ''
    pageSize: params.pageSize ? 10
    pageIndex: params.pageIndex ? 1
    sessionkey: req.cookies['__t'] ? 'sessionKey'
  }
  .then (data)->
    if not data.status
      return res.json {status:false,data: data}
    res.json {data: data}
  .catch next

router.post '/' ,
module.exports = router